//
//  LXRefreshControl.swift
//  weibo
//
//  Created by lixin on 16/4/22.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

enum LXRefreshControlType: Int{
    
    //正常状态
    case Normal = 0
    
    //松手刷新
    case Pulling = 1
    
    //刷新中
    case Refreshing = 2
}

//控件高度
private let RefreshHeight: CGFloat = 50

class LXRefreshControl: UIControl {
    
    var LXState:LXRefreshControlType = .Normal{
        
        didSet{
            switch LXState{
                
            case .Normal:
                
                messageLabel.text = "T(n_n)T 下拉刷新"
                
                if oldValue == .Refreshing{
                    
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        
                        self.scrollView?.contentInset.top -= RefreshHeight
                    })
                }

            case .Pulling:
                
                messageLabel.text = "Y(u_u)Y 松手更新"
                
            case .Refreshing:
                
                messageLabel.text = "~m(>.<)m~ 加载中"
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    self.scrollView?.contentInset.top += RefreshHeight
                    
                    }, completion: { (_) -> Void in
                        
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                })
            }
        }
    }
    
    //scrollView
    var scrollView: UIScrollView?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 供外界调用(停止刷新)
    func endRefreshing(){
        
        LXState = .Normal
    }
    
    // MARK: - 视图设置
    private func setUI(){
        
        self.backgroundColor = UIColor.whiteColor()
        
        addSubview(messageLabel)
        
        
    }
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        self.frame = CGRect(x: 0, y: -RefreshHeight, width: SCREENW, height: RefreshHeight)
       
        messageLabel.frame = CGRect(x: 0, y: 0, width: SCREENW, height: RefreshHeight)
    }

    override func willMoveToSuperview(newSuperview: UIView?) {
       
        if let scrollView = newSuperview as? UIScrollView{
            
            self.scrollView = scrollView
            
            scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
            
        }
    }
   
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let contentMargin = -(RefreshHeight + scrollView!.contentInset.top)
        
        let contentOffsetY = scrollView?.contentOffset.y
       
        if scrollView!.dragging{
            
            if contentOffsetY > contentMargin && LXState == .Pulling{
                
                LXState = .Normal
                
            }else if contentOffsetY <= contentMargin && LXState == .Normal{
                
                LXState = .Pulling
            }
            
        }else{
            
            if LXState == .Pulling{
                
                LXState = .Refreshing
            }
            
        }
        
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textAlignment = .Center
        
        label.textColor = ThemeColor
        
        return label
    }()
    
    //移除KVO
    deinit{
        
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
}

